package com.mach.platform.domain;

import com.google.common.base.Splitter;
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

/**
 * @author Administrator
 * @Description: 控制 流程 信息储存
 * @Package: com.mach.platform.domain
 * @time 2018/11/16 0016 15:35
 */
@Data
@Entity
@Table(name = "ctrl_process")
public class CtrlProcess implements Serializable {
	private static final long serialVersionUID = -2677832520444284648L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private String name;

	@Column(name = "dev_id")
	private Long devId;

	/**
	 * 手动填入模式，不使用外键，避免麻烦
	 *  id|name
	 */
	private String process;

	private int group;

    private String uuid;

	@Transient
	private List processes =  new ArrayList();

	/**
	 * 运行时间
	 */
	@Transient
	private int runtime;
	/**
	 * 开机次数
	 */
	@Transient
	private int launchTime;
	/**
	 * 设备状态
	 */
	@Transient
	private int runStatus;

	public List getProcesses ( ) {
        Optional.ofNullable(this.process).ifPresent(source -> {
            List<String> originList = Splitter.on(",").splitToList(this.process);
            if (originList.size() > 0) {
                originList.stream().forEach(item -> {
                    List<String> splitToList = Splitter.on("|").splitToList(item);
                    Map newMap = new HashMap<>(3);
                    newMap.put("devId", splitToList.get(0));
                    newMap.put("name", splitToList.get(1));
                    if (splitToList.size() == 3) {
                        newMap.put("uuid", splitToList.get(2));
                    }
                    if (splitToList.size() == 4) {
                        newMap.put("uuid", splitToList.get(2));
                        newMap.put("cmd", Splitter.on("-").splitToList(splitToList.get(3)));
                    }
                    this.processes.add(newMap);
                });
            }
        });
		return processes;
	}

}
